package com.itheima.controller;

import com.itheima.common.R;

import com.itheima.pojo.dto.AddressBookDto.AddressBookAddDto;
import com.itheima.pojo.dto.AddressBookDto.AddressBookUpdateDto;
import com.itheima.pojo.vo.addressBookVo.AddressBookByIdVo;
import com.itheima.pojo.vo.addressBookVo.AddressBookUpdateDefaultVo;
import com.itheima.pojo.vo.addressBookVo.GetAddressBookVo;
import com.itheima.pojo.vo.addressBookVo.GetDefaultAddressBookVo;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @PostMapping
    public R  add(@RequestBody AddressBookAddDto addressBook){

        boolean add = addressBookService.add(addressBook);
        if (add){
            return R.success("添加成功");
        }
        return R.error("添加失败");

    }
    @PutMapping
    public R update(@RequestBody AddressBookUpdateDto addressBook){
        boolean update = addressBookService.update(addressBook);
        if (update){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    @DeleteMapping
    public  R delete(Integer[]ids){
        boolean delete = addressBookService.delete(ids);
        if (delete){
            return  R.success("删除成功");
        }
        return R.error("删除失败");
    }
    @GetMapping("/default")
    public R  findDef(){

        GetDefaultAddressBookVo addressBook = addressBookService.selectByIsDefault();
        return R.success(addressBook);
    }
    @PutMapping("/default")
    public R updateDef(@RequestBody AddressBookUpdateDto book){
        String id = book.getId();
        GetDefaultAddressBookVo addressBook=addressBookService.updateDef(id);
        return R.success(addressBook);
    }
    @GetMapping("/list")
    public R selectAll(){
        List<GetAddressBookVo> list = addressBookService.selectAll();
        return R.success(list);
    }
    @GetMapping
    public R selectById(String id){
        AddressBookByIdVo addressBook = addressBookService.selectById(id);
        return R.success(addressBook);

    }

}
